using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using CWM.Skinn;

/// <summary>
/// CC3导出模型头部SubMesh类型  9
/// </summary>
public enum CC3SubMeshHeadType
{
    Tongue,
    Head,
    Eyelash,
    Upper_Teeth,
    Lower_Teeth,
    Eye_R,
    Cornea_R,
    Eye_L,
    Cornea_L,
}

/// <summary>
/// CC3导出模型身体SubMesh类型  4
/// </summary>
public enum CC3SubMeshBodyType
{
    Body,
    Arm,
    Leg,
    Nails,
}

public class SkinnProCustomConfig
{
    #region CC3 Struct Key

    public const string BodyRoot = "CC_Base_BoneRoot";

    public const string BodyMesh = "CC_Base_Body";
    public const string EyeMesh = "CC_Base_Eye";
    public const string TeethMesh = "CC_Base_Teeth";
    public const string TongueMesh = "CC_Base_Tongue";

    public const string SplitSuffix = "_Split";

    #endregion
}

public class SkinnProCustomMain
{
    /// <summary>
    /// 1.分割SubMeshes
    /// </summary>
    public static void SplitSubMeshes()
    {
        if (!SelectComponents(typeof(SkinnedMeshRenderer)))
        {
            return;
        }

        SkinnContextCommands.RendererSplit();
        Debug.Log("Split done");
    }

    /// <summary>
    /// 2.提取BlendShapes
    /// 在Skinn Pro的基础上进行了一些修改，跳过Context，且拿到新生成的MeshRenderers
    /// </summary>
    public static List<GameObject> ExtractBlendShapes(string typeKey)
    {
        if (!SelectAComponent(typeof(SkinnedMeshRenderer)))
        {
            return null;
        }

        SkinnContextCommands.SkinnedMeshRendererShapesExtractAll();
        Debug.Log("Extract done : " + typeKey);

        return SkinnContextCommands.GetExtractGameObjsList();
    }

    /// <summary>
    /// 3.根据当前的表情烘焙出MeshRenderer
    /// </summary>
    public static void Bake2MeshRenderer()
    {
        if (!SelectComponents(typeof(SkinnedMeshRenderer)))
        {
            return;
        }

        SkinnContextCommands.SkinnedMeshBake();
        Debug.Log("Bake done");
    }

    /// <summary>
    /// 3.1.打开ShapeLoad
    /// </summary>
    /// <param name="skinnedMeshRenderer"></param>
    public static SkinnShapeLoaderWizard ShapeLoad2SkinMeshRenderer(SkinnedMeshRenderer skinnedMeshRenderer)
    {
        SkinnShapeLoaderWizard.ShowWizard(skinnedMeshRenderer);

        return SkinnShapeLoaderWizard.wizard;
    }

    /// <summary>
    /// 4.清理不需要的BlendShapes信息
    /// </summary>
    public static void RemoveBlendShapes()
    {
        if (!SelectComponents(typeof(SkinnedMeshRenderer)))
        {
            return;
        }

        SkinnContextCommands.RendererShapesRemoveAll();
        Debug.Log("Remove done");
    }

    /// <summary>
    /// 5.合并BlendShapes网格
    /// </summary>
    public static void CombineBlendShapesMesh()
    {
        if (!SelectComponents(typeof(SkinnedMeshRenderer)))
        {
            return;
        }

        SkinnContextCommands.RendererCombineKeep();
        Debug.Log("Combine done");
    }

    /// <summary>
    /// 选择带有某种类型组件的物体
    /// </summary>
    private static bool SelectAComponent(Type componentType)
    {
        var select = Selection.activeGameObject;
        if (select == null || !select.GetComponent(componentType))
        {
            Debug.Log("Please select a typeof " + componentType.Name);

            return false;
        }

        return true;
    }

    private static bool SelectComponents(Type componentType)
    {
        var selects = Selection.gameObjects;
        if (selects == null)
        {
            Debug.Log("Please select gameObjects");

            return false;
        }

        foreach (var select in selects)
        {
            if (!select.GetComponent(componentType))
            {
                Debug.Log("Please select gameObjects with typeof " + componentType.Name);

                return false;
            }
        }

        return true;
    }
}
